#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import urls
import os
import os.path

def init_options():
    tornado.options.define('port', default=8000, type=int)
    tornado.options.define('worker', default=1, type=int)
    tornado.options.define('debug', default=True, type=bool)
    tornado.options.parse_command_line()
    return tornado.options.options




def init_global():
    # 设置全局socket超时时间为30秒
    import socket
    socket.setdefaulttimeout(30)

_options = init_options()
init_global()

settings = {
    'debug': _options.debug,
    'cookie_secret':"32oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
    "login_url": "/log",
    'template_path':os.path.join(os.path.dirname(__file__), "templates"),
    'static_path':os.path.join(os.path.dirname(__file__), "/home/liuyue/svn/wodfan/blog"),
    #'static_path': os.path.join(os.path.dirname(__file__), "/opt/web/Statis/web"),
    #'static_path': os.path.join(os.path.dirname(__file__), "/home/leeray/workspaces/Python/wanalytics/Statis/web"),
    #'static_url_prefix': '/static/'
}

application = tornado.web.Application(urls.urls, **settings)



class BaseHandler(tornado.web.RequestHandler):
    @property
    def db(self):
        return self.application.db

    def get_current_user(self):
        user_id = self.get_secure_cookie("user")
        if not user_id: return None
        return self.db.get("SELECT * FROM authors WHERE id = %s", int(user_id))

if __name__ == "__main__":
    _httpserver = tornado.httpserver.HTTPServer(application)
    _httpserver.bind(_options.port)
    _httpserver.start(_options.worker)
    tornado.ioloop.IOLoop.instance().start()

